package com.qlk.odcinterface.rest;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.resteasy.annotations.Form;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.qlk.common.utils.serialize.JsonHelper;
import com.qlk.common.utils.wrap.WrapMapper;
import com.qlk.common.utils.wrap.Wrapper;
import com.qlk.odcinterface.domain.PmsProductPromotion;
import com.qlk.odcinterface.domain.PmsProductPromotionChannel;
import com.qlk.odcinterface.domain.PmsProductPromotionItem;
import com.qlk.odcinterface.domain.query.PmsProductPromotionQuery;
import com.qlk.odcinterface.manager.PmsProductPromotionManager;
import com.qlk.odcinterface.sdk.api.request.OdcRequest;
import com.qlk.odcinterface.sdk.api.request.PmsProductPromotionRequest;
import com.qlk.odcinterface.sdk.api.response.PmsProductPromotionResponse;
import com.qlk.odcinterface.service.PmsProductPromotionChannelService;
import com.qlk.odcinterface.service.PmsProductPromotionItemService;
import com.qlk.odcinterface.service.PmsProductPromotionService;

/**
 * 单品促销表REST服务：提供有关单品促销表的接口
 * 
 * @author zhaoyanan
 * @version 2016-4-14 13:22:59
 * 
 */
@Component
@Path("services")
@Produces({ MediaType.APPLICATION_JSON })
/*@Consumes({ MediaType.APPLICATION_JSON })*/
public class PmsProductPromotionResource {

    private final Log logger = LogFactory.getLog(this.getClass());

    @Autowired
    private PmsProductPromotionService pmsProductPromotionService; 
    @Autowired
    private PmsProductPromotionChannelService pmsProductPromotionChannelService;
    @Autowired
    private PmsProductPromotionItemService pmsProductPromotionItemService;

    /**
     * 查询单品促销表信息
     * 
     * @param request
     *            单品促销表请求参数
     * @return 单品促销表返回对象
     * 
     */
    @POST
    @Path("/pmsProductPromotion/getPmsProductPromotion")
    public Wrapper<?> getPmsProductPromotion(PmsProductPromotionRequest request) {
        if (null == request || null == request.getId()) {
            this.logger.error("getPmsProductPromotion 传入参数有误");
            return WrapMapper.illegalArgument();
        }

        try {
            PmsProductPromotion pmsProductPromotion = pmsProductPromotionService.getPmsProductPromotionById(request.getId());
            PmsProductPromotionResponse response = convert(pmsProductPromotion);
            return WrapMapper.ok().result(response);
        } catch (Exception e) {
            this.logger.error("查询单品促销表数据异常", e);
            return WrapMapper.error();
        }
    } 

    // 数据转换
    private PmsProductPromotionResponse convert(PmsProductPromotion pmsProductPromotion) {
        if (null == pmsProductPromotion) {
            return null;
        }

        PmsProductPromotionResponse pmsProductPromotionResponse = new PmsProductPromotionResponse();
        BeanUtils.copyProperties(pmsProductPromotion, pmsProductPromotionResponse);
        return pmsProductPromotionResponse;
    }

    // 数据转换
    private List<PmsProductPromotionResponse> convertList(List<PmsProductPromotion> pmsProductPromotions) {
        if (CollectionUtils.isEmpty(pmsProductPromotions)) {
            return null;
        }

        List<PmsProductPromotionResponse> list = new ArrayList<PmsProductPromotionResponse>(pmsProductPromotions.size());
        for (PmsProductPromotion pmsProductPromotion : pmsProductPromotions) {
            list.add(convert(pmsProductPromotion));
        }
        return list;
    } 
    /**
     * 查询单品促销表信息
     * 
     * @param request
     *            单品促销表请求参数
     * @return 单品促销表返回对象
     * 
     */
    @POST
    @Path("/pmsProductPromotion/syncPmsProductPromotion")
    public Wrapper<?> syncPmsProductPromotion(@Form OdcRequest request) {
        if (null == request) {
            this.logger.error("getPmsProductPromotion 传入参数有误");
            return WrapMapper.illegalArgument();
        }
       
        String content = request.getContent();
        PmsProductPromotionRequest pmsProductPromotionRequest = JsonHelper.toBean(content, PmsProductPromotionRequest.class);
        Integer isStatusUpdate = Integer.parseInt(request.getIsStatusUpdate());
        String preStatus = request.getPreStatus();
        Wrapper<?> rs = pmsProductPromotionService.syncPmsProductPromotion(pmsProductPromotionRequest,isStatusUpdate,preStatus);       
        return rs;
    }
}
